package engine.graphics.popup;

import util.List;

import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Graphics;

import engine.data.Player;
import engine.data.Technology;
import engine.graphics.MapCanvasEngine;
import engine.graphics.misc.GraphicsCustomFont;

public final class PopupTechnology extends TabPopup {

	private static final byte TECHS_PANEL = 1;

	private static int[][][] techsCursor;

	private List techs;

	private byte vTechsIndex = 0;

	private byte vTechsSubIndex = 0;

	private Player player;

	public byte selectedTechnology = -1;

	public PopupTechnology(final Player gamePlayer, final List selectedTechs) {
		tabCursor = new int[][][] { { { 1, 1 }, { 17, 11 } }, { { 20, 1 }, { 63, 11 } } };
		this.player = gamePlayer;
		this.techs = selectedTechs;
		techsCursor = initCursor((byte) this.techs.size(), (byte) 10);
	}

	public void show(final int screenX, final int screenY) {
		Graphics graphics = MapCanvasEngine.canvas.graphics;
		super.show(screenX, screenY, graphics);
		GraphicsCustomFont.drawString(graphics, "[X] Technologies", this.x + 2, this.y + 3);
		this.hIndex = TECHS_PANEL;
		showHCursor();
		showPanel();
	}

	public void showTechsPanel() {
		Graphics graphics = MapCanvasEngine.canvas.graphics;
		int j = MARGIN_PANEL_HEIGHT;
		byte max = (byte) Math.min(this.techs.size(), ((vTechsSubIndex + 1) * 11));
		byte tech;
		short turns;
		for (byte i = (byte) (vTechsSubIndex * 11); i < max; i++) {
			tech = ((Byte) this.techs.get(i)).byteValue();
			GraphicsCustomFont.drawString(graphics, Technology.getName(tech), this.x + 5, this.y + j);
			turns = (short) Math.max(0, player.getMaxRP(tech) - player.knowledgePoints);
			GraphicsCustomFont.drawString(graphics, "" + turns, this.x + 117, this.y + j);
			j += 10;
		}
		if (this.techs.size() > 11)
			GraphicsCustomFont.drawString(graphics, "...", this.x + 115, this.y + 125);
		this.panel = TECHS_PANEL;
	}

	public void keyPressed(final int key) {
		int action = MapCanvasEngine.canvas.getGameAction(key);
		if (action != 0)
			switch (action) {
				case Canvas.DOWN:
					if (this.panel == TECHS_PANEL)
						vTechsIndex = moveCursor(techsCursor, vTechsIndex, (byte) 1, (byte) 12);
					break;
				case Canvas.UP:
					if (this.panel == TECHS_PANEL)
						vTechsIndex = moveCursor(techsCursor, vTechsIndex, (byte) -1, (byte) 12);
					break;
				case Canvas.LEFT:
					this.showNextTab((byte) -1);
					break;
				case Canvas.RIGHT:
					this.showNextTab((byte) 1);
					break;
				case Canvas.FIRE:
					switch (this.panel) {
						case TECHS_PANEL:
							if (vTechsIndex % 12 == 11
									|| (vTechsIndex == techsCursor.length - 1 && this.techs.size() > 11)) {
								vTechsIndex += 12;
								vTechsSubIndex++;
								if (vTechsSubIndex * 11 > this.techs.size()) {
									vTechsSubIndex = 0;
									vTechsIndex = 11;
								}
								showPanel();
								if (vTechsIndex >= techsCursor.length)
									vTechsIndex = (byte) (techsCursor.length - 1);
								showVCursor(techsCursor, vTechsIndex);
							} else {
								if (!hCursorScope) {
									selectedTechnology = ((Byte) this.techs.get((vTechsSubIndex * 11) + vTechsIndex
											% 12)).byteValue();
									this.hide();
								}
							}
							break;
						case CLOSE_PANEL:
							this.hide();
							break;
						default:
							break;
					}
					break;
				default:
					break;
			}
	}

	protected void showPanel() {
		super.showPanel();
		if (this.hIndex == TECHS_PANEL)
			showTechsPanel();
		this.repaint();
	}

}
